﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using IESN.Common;
using IESN.Enum;
using IESN.BusinessCore;
using IESN.BaseForms;

namespace IESN.DM
{
    /// <summary>
    /// 电镀时间设定
    /// </summary>
    public partial class DMMappingFrm : EditBaseFrm
    {
        private List<ComboboxCommonObj> aNodeTypeList = new List<ComboboxCommonObj>();
        private List<ComboboxCommonObj> aRwTypeList = new List<ComboboxCommonObj>();
        private List<ComboboxCommonObj> aDmMappingList = new List<ComboboxCommonObj>();

        /// <summary>
        /// 构造函数
        /// </summary>
        public DMMappingFrm()
        {
            InitializeComponent();
            CurrentEntity = new DMMapping();
        }
        
        protected override void InitControl()
        {
            base.InitControl();

            IsComboBoxSelectValue = true;
            InitCombobox();
        }

        private void InitCombobox()
        {
            aNodeTypeList.Add(new ComboboxCommonObj() { ID = ((int)DmNodeType.Parent).ToString(), Name = "父结点" });
            aNodeTypeList.Add(new ComboboxCommonObj() { ID = ((int)DmNodeType.Child).ToString(), Name = "子结点" });
            cbbDmNodeType.DataSource = aNodeTypeList;
            cbbDmNodeType.ValueMember = "ID";
            cbbDmNodeType.DisplayMember = "Name";

            aRwTypeList.Add(new ComboboxCommonObj() { ID = ((int)DmRWType.Read).ToString(), Name = "读" });
            aRwTypeList.Add(new ComboboxCommonObj() { ID = ((int)DmRWType.Write).ToString(), Name = "写" });
            aRwTypeList.Add(new ComboboxCommonObj() { ID = ((int)DmRWType.ResetBit).ToString(), Name = "重置设置位" });
            aRwTypeList.Add(new ComboboxCommonObj() { ID = ((int)DmRWType.SetBit).ToString(), Name = "设置位" });
            cbbDmRWType.DataSource = aRwTypeList;
            cbbDmRWType.ValueMember = "ID";
            cbbDmRWType.DisplayMember = "Name";

            foreach (var aDmMapping in GlobalCache.GetInstance.DmMapping)
            {
                aDmMappingList.Add(new ComboboxCommonObj() { ID = aDmMapping.Key, Name = aDmMapping.Value.DMAddressName });
            }
            cbbRelationID.AutoCompleteCustomSource.AddRange(aDmMappingList.Select(p => p.ID).ToArray());
            cbbRelationID.AutoCompleteSource = AutoCompleteSource.ListItems;
            cbbRelationID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cbbRelationID.DataSource = aDmMappingList;
            cbbRelationID.ValueMember = "ID";
            cbbRelationID.DisplayMember = "Name";
        }

        #region GridView行切换

        private void dgvDmMapping_SelectionChanged(object sender, EventArgs e)
        {
            CommonHelper<DMMapping> cHelper = new CommonHelper<DMMapping>();

            if (dgvDmMapping.SelectedRows != null && dgvDmMapping.SelectedRows.Count > 0)
            {
                DataGridViewRow gridrow = dgvDmMapping.SelectedRows[0];
                DataRowView row_view = (DataRowView)gridrow.DataBoundItem;
                CurrentEntity = cHelper.FillModel(row_view.Row);

                ControlHelper.SetObjectValueToControl(CurrentEntity, panelContent);
            }
        }

        #endregion

        #region 操作完成后处理

        /// <summary>
        /// 新增后处理
        /// </summary>
        protected override void AfterInserted()
        {
            base.AfterInserted();
            InsertGrid();
        }

        /// <summary>
        /// 编辑后处理
        /// </summary>
        protected override void AfterUpDated()
        {
            base.AfterUpDated();
            UpdateGrid();
        }

        /// <summary>
        /// 插入到DataGrid
        /// </summary>
        private void InsertGrid()
        {
            BindingSource bs = dgvDmMapping.DataSource as BindingSource;
            DataTable dt = bs.DataSource as DataTable;

            CommonHelper<DMMapping> cHelper = new CommonHelper<DMMapping>();
            DataRow newDr = cHelper.FillDataRow(CurrentEntity as DMMapping, dt);
            DataTable cloneTb = dt.Copy();

            cloneTb.Rows.Add(newDr.ItemArray);

            BindingSource newBS = new BindingSource();
            newBS.DataSource = cloneTb;
            dgvDmMapping.DataSource = newBS;
            dgvDmMapping.Rows[cloneTb.Rows.Count - 1].Selected = true;
            dgvDmMapping.Refresh();
        }

        /// <summary>
        /// 更新到DataGrid
        /// </summary>
        private void UpdateGrid()
        {
            BindingSource bs = dgvDmMapping.DataSource as BindingSource;
            DataTable dt = bs.DataSource as DataTable;
            DataRow[] drs = dt.Select("ID='" + CurrentEntity.ID + "'");

            if (drs != null && drs.Length > 0)
            {
                DataRow dr = drs[0];

                CommonHelper<DMMapping> cHelper = new CommonHelper<DMMapping>();
                DataRow newDr = cHelper.FillDataRow(CurrentEntity as DMMapping, dt);

                drs[0].ItemArray = newDr.ItemArray;
                dgvDmMapping.Refresh();
            }
        }

        #endregion

        private void dgvDmMapping_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }

            var aCol = dgvDmMapping.Columns[e.ColumnIndex];
            if (aCol.DataPropertyName.ToString() == "DmNodeType")
            {
                e.Value = e.Value.ToString() == "1" ? "父结点" : "子结点";
            }
            else if (aCol.DataPropertyName.ToString() == "DmRWType")
            {
                switch (e.Value.ToString())
                {
                    case "1":
                        e.Value = "读";
                        break;
                    case "2":
                        e.Value = "写";
                        break;
                    case "3":
                        e.Value = "重置设置位";
                        break;
                    case "4":
                        e.Value = "设置位";
                        break;
                    default:
                        break;
                }
            }
        }

    }
}
